package cn.ls.service.impl;

import cn.ls.dao.SvnMapper;
import cn.ls.pojo.SvnDO;
import cn.ls.service.SvnService;
import cn.ls.util.SvnUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.SVNLogEntryPath;
import org.tmatesoft.svn.core.io.SVNRepository;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

@Service
public class SvnServiceImpl implements SvnService {
	private static final Logger logger = LoggerFactory.getLogger(SvnServiceImpl.class);

	@Resource
	SvnMapper svnMapper;

	/***
	 * 插入svn提交日志记录到数据记录表
	 *
	 * @param
	 * @param svnUrl
	 * @param projectName
	 * @param starDate
	 * @param endDate
	 * @return void
	 * @date 2021/5/6
	*/
	@Override
	public void addSvnLog(String svnUrl, String projectName, String starDate, String endDate){
		SVNRepository repository = null;
		SVNLogEntry[] svnLogs = null;

		try {
			repository = SvnUtil.getSvnRepository(svnUrl);
		} catch (Exception e) {
			logger.error("Create repository error",e);
		}
		try {
			DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
			svnLogs = getSvnLogs(repository, dateFormat.parse(starDate), dateFormat.parse(endDate));
		} catch (Exception e) {
			logger.error("Get svn logs error",e);
		}
		// 数据插入
		if (svnLogs != null) {
			for (SVNLogEntry svnLogEntry : svnLogs) {
				SvnDO svnDO = new SvnDO();
				svnDO.setSvnUrl(svnUrl);
				String author = svnLogEntry.getAuthor();
				Date date = svnLogEntry.getDate();
				String message = svnLogEntry.getMessage();
				Map<String, SVNLogEntryPath> changedPaths = svnLogEntry.getChangedPaths();
				Collection<SVNLogEntryPath> values = changedPaths.values();
				long revision = svnLogEntry.getRevision();

				int count = svnMapper.selectCountByRevision(revision);
				if (count != 0) {
					continue;
				}
				for (SVNLogEntryPath value : values) {
					String type = String.valueOf(value.getType());
					String path = value.getPath();
					// 历史下载路径，这个应该要要根据实际配置的来拼接修改
					String version_file_url = "http://192.168.1.32/BILLING/!svn/ver/" + revision + path;
					svnDO.setChangedAuthor(author);
					svnDO.setChangedDate(date);
					svnDO.setChangedRemark(message);
					svnDO.setRevision(revision);
					svnDO.setChangedType(type);
					svnDO.setChangedPaths(path);
					svnDO.setProjectName(projectName);
					svnDO.setVersionFileUrl(version_file_url);
					svnMapper.insertSelective(svnDO);
				}
			}
		}
	}
	/***
	 * 获取一段时间内，所有的commit记录
	 * @param starDate	开始时间
	 * @param endDate	结束时间
	 * @return
	 * @throws SVNException
	 */
	private SVNLogEntry[] getSvnLogs(SVNRepository repository,Date starDate, Date endDate) throws SVNException{
		long startRevision = repository.getDatedRevision(starDate);
		long endRevision = repository.getDatedRevision(endDate);
		@SuppressWarnings("unchecked")
		Collection<SVNLogEntry> logEntries = repository.log(new String[]{""}, null,
				startRevision, endRevision, true, true);
		SVNLogEntry[] svnLogEntries = logEntries.toArray(new SVNLogEntry[0]);
		return svnLogEntries;
	}
}